﻿using System.Drawing;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;
using Application = System.Windows.Application;

namespace Xcj.Mes.Core.Common
{
    public class SystemTray
    {
        public static SystemTray Instance;

        /// <summary>
        /// 静态构造函数,在类第一次被创建或者静态成员被调用的时候调用
        /// </summary>
        static SystemTray()
        {
            Instance = new SystemTray();
        }

        public NotifyIcon Ni { get; set; }

        private SystemTray()
        {
            Ni = new System.Windows.Forms.NotifyIcon();

            //Ni.Icon = new Icon(
            //   System.Windows.Application.GetResourceStream(
            //        new Uri("pack://application:,,,/Images/Logo3_48x48.ico", UriKind.RelativeOrAbsolute)
            //   //new Uri("app.ico", UriKind.Relative)
            //   ).Stream
            // );

            Ni.Icon = SystemIcons.WinLogo;
            Ni.Visible = true;
            Ni.MouseClick += Ni_MouseClick;

            System.Windows.Controls.ContextMenu NotifyIconMenu = (System.Windows.Controls.ContextMenu)Application.Current.FindResource("NotifyIconMenu");
            NotifyIconMenu.IsOpen = false;
        }

        private void Ni_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if ((System.Windows.Application.Current.MainWindow == null) || (System.Windows.Application.Current?.Dispatcher == null))
                return;

            ThreadPool.QueueUserWorkItem(delegate
            {
                try
                {
                    SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Application.Current.Dispatcher));
                    SynchronizationContext.Current?.Post(p =>
                    {
                        System.Windows.Controls.ContextMenu NotifyIconMenu = (System.Windows.Controls.ContextMenu)Application.Current.FindResource("NotifyIconMenu");
                        NotifyIconMenu.IsOpen = false;
                        if (e.Button == MouseButtons.Right)
                        {
                            if (Application.Current.MainWindow.Visibility == System.Windows.Visibility.Visible)
                                NotifyIconMenu.Items[0] = "隐藏窗口";
                            else
                                NotifyIconMenu.Items[0] = "显示窗口";
                            NotifyIconMenu.IsOpen = true;
                        }
                        else if (e.Button == MouseButtons.Left)
                        {
                            if (Application.Current.MainWindow.Visibility == System.Windows.Visibility.Visible)
                            {
                                if (Application.Current.MainWindow.WindowState == WindowState.Minimized)
                                {
                                    Application.Current.MainWindow.WindowState = System.Windows.WindowState.Maximized;
                                    Application.Current.MainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                                    Application.Current.MainWindow.Show();
                                }
                                else
                                    Application.Current.MainWindow?.Hide();
                            }
                            else
                            {
                                Application.Current.MainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                                Application.Current.MainWindow.WindowState = System.Windows.WindowState.Maximized;
                                Application.Current?.MainWindow?.Show();
                            }
                        }
                    }, null);
                }
                catch { }
            });
        }

        /// <summary>
        /// 销毁系统托盘图标的资源
        /// </summary>
        public void DisposeNotifyIcon()
        {
            Ni?.Dispose();
        }
    }
}
